/// <summary>
/// GameCam.cs by Roy Mitchell 09/07/2011
/// This camera script should be applied to your main camera.
/// This script will function in both Orphographic and Perspective views which you can toggle between in the inspector
/// Field of View option is primarily set for the Orphographic view but can be used in perspective view to adjust the game FOV
/// The script will look for the "Player" tag if no tag is set for the camera to be set by.
/// This script isnt designed to be a specific set focus camera hence there not being no LookAt(target) implementation.
/// </summary>


using UnityEngine;
using System.Collections;

public class GameCam : MonoBehaviour
{
	private Transform _GameCamera;
	private Transform targetPos;
	private Vector3 vec3CamPos;
	private float targetX;
	private float targetY;
	private float targetZ;
	private Vector3 newCamPos;
	private float zoomInOut = 0f;					// This works best in perspective view. 
	
	//public string targetObjectTag;
	public Transform target;
	public bool OrphographicView;
	public float camTiltUpDown = 135f;
	public float camPanLeftRight = -90f;
	public float camRollLeftRight = 45f;
	public float camRotateUpDown = 360f;
	public float cameraUpDown = 24f;
	public float cameraForwardBack = -24f;
	public float cameraLeftRight = 12f;
	public float zoomSpeedUpDownArrowKeys = 1f;		// Speed in which to zoom the camera in and out when using the UP/DOWN arrow keys
	public float fieldOfView = 20;
	
	void Start()
	{
		this.camera.isOrthoGraphic = OrphographicView;
		
		if(target == null)
		{
			Debug.LogWarning("No target selected");
		}
		targetPos = target;//GameObject.FindWithTag(targetObjectTag);	

		_GameCamera = transform;

		vec3CamPos = targetPos.transform.position;
		targetX = vec3CamPos.x;
		targetY = vec3CamPos.y;
		targetZ = vec3CamPos.z;
		newCamPos = new Vector3(targetX, targetY, targetZ);		
	}
	
	void LateUpdate()
	{
		if(!OrphographicView)
		{
			if(Input.GetKey(KeyCode.UpArrow))
				zoomInOut -= zoomSpeedUpDownArrowKeys;
			if(Input.GetKey(KeyCode.DownArrow))
				zoomInOut += zoomSpeedUpDownArrowKeys;
		}
		
		camera.isOrthoGraphic = OrphographicView;
		
		vec3CamPos = targetPos.transform.position;
		targetX = vec3CamPos.x + cameraLeftRight + (zoomInOut*0.5f);
		targetY = vec3CamPos.y + cameraUpDown + zoomInOut;
		targetZ = vec3CamPos.z + cameraForwardBack - zoomInOut;
				
		if(OrphographicView)
		{
			camera.orthographicSize = fieldOfView;
		}
		if(!OrphographicView)
		{
			camera.fieldOfView = fieldOfView;	
		}
		
		_GameCamera.rotation = new Quaternion(camTiltUpDown, camPanLeftRight, camRollLeftRight, camRotateUpDown);

		newCamPos = new Vector3(targetX, targetY, targetZ);
		
		_GameCamera.transform.position = newCamPos;
	}
}

